﻿using Api.BLL;
using Api.Common;
using Api.Model;
using Api.Web.helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Api.Web.Areas.Admin.Controllers
{
    public class ErrorCodeController : AdminController
    {
        public ActionResult List(int? projectId)
        {
            if (projectId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View();
            }

            var list = ErrorCodeBLL.Current.GetListNoTrack(s => s.UserId == userId && s.ProjectId == projectId);
            var categorys = CategoryBLL.Current.GetListNoTrack(s => s.ProjectId == projectId && s.UserId == userId);
            if (list == null || categorys == null)
            {
                if (list == null)
                {
                    ViewBag.Error = "數據庫錯誤";
                    return View();
                }
            }
            ViewBag.Categorys = categorys;
            return View(list);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Edit(ErrorCode errorCode)
        {
            if (errorCode.ProjectId == 0)
            {
                AjaxMsgHelper.ResponseFail("缺少projectId");
                return;
            }
            bool flag = true;
            if (!string.IsNullOrEmpty(errorCode.Code))
            {
                if (errorCode.Id > 0)
                {
                    try
                    {
                        ErrorCodeBLL.Current.UpdateIgnore(s => new { s.CreateDate, s.ProjectId, s.UserId }, errorCode);
                    }
                    catch
                    {
                        flag = false;
                    }
                }
                else
                {
                    try
                    {
                        errorCode.ProjectId = projectId;
                        errorCode.UserId = userId;
                        errorCode.CreateDate = DateTime.Now;
                        ErrorCodeBLL.Current.Add(errorCode);
                    }
                    catch
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    AjaxMsgHelper.ResponseSuccess("", null, Url.Action("List", new { Area = "Admin", projectId = errorCode.ProjectId }));
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗，請重試");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請填寫名稱");
            }
        }
        public ActionResult Create(int? projectId)
        {
            if (projectId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View();
            }
            return View();
        }
        public ActionResult Edit(int? projectId, int? id)
        {
            if (id == null || projectId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View("Create");
            }
            ErrorCode ErrorCode = ErrorCodeBLL.Current.GetModel(s => s.Id == id && s.ProjectId == projectId && s.UserId == userId);
            if (ErrorCode == null)
            {
                ViewBag.Error = "數據不存在";
                return View("Create");
            }
            return View("Create", ErrorCode);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Delete(params int[] ids)
        {
            if (ids.Length > 0)
            {
                var list = ErrorCodeBLL.Current.GetList(s => ids.Contains(s.Id) && s.UserId == userId);
                if (list == null)
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                    return;
                }
                bool result = true;
                try
                {
                    ErrorCodeBLL.Current.DeleteList(list);
                }
                catch
                {
                    result = false;
                }
                if (result)
                {
                    AjaxMsgHelper.ResponseSuccess("刪除成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請選擇一項");
            }
        }
    }
}